package com.skyline.user.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.skyline.base.type.Activity;
import com.skyline.user.model.User;
import com.skyline.user.type.Sex;

//TODO:加注释
public class UserMapper implements RowMapper<User> {

	private final static String COLUMN_ACTIVITY = "activity";
	private final static String COLUMN_AUTHORITY = "authority";
	private final static String COLUMN_BIRTHDAY = "birthday";
	private final static String COLUMN_CITY = "city";
	private final static String COLUMN_COUNTRY = "country";
	private final static String COLUMN_CREATE_TIME = "createTime";
	private final static String COLUMN_SECURITY_EMAIL = "securityEmail";
	private final static String COLUMN_EMAIL = "email";
	private final static String COLUMN_GRADUATE_SCHOOL = "graduateSchool";
	private final static String COLUMN_JOB = "job";
	private final static String COLUMN_LAST_LOGIN_IP = "lastLoginIp";
	private final static String COLUMN_LAST_LOGIN_TIME = "lastLoginTime";
	private final static String COLUMN_MOBILE_PHONE = "mobilephone";
	private final static String COLUMN_MSN = "msn";
	private final static String COLUMN_NAME = "name";
	private final static String COLUMN_NICKNAME = "nickname";
	private final static String COLUMN_PORTRAIT = "portrait";
	private final static String COLUMN_PROVINCE = "province";
	private final static String COLUMN_QQ = "qq";
	private final static String COLUMN_SECURITY_ANSWER = "securityAnswer";
	private final static String COLUMN_SECURITY_QUESTION = "securityQuestion";
	private final static String COLUMN_SEX = "sex";
	private final static String COLUMN_SITE_ID = "siteId";
	private final static String COLUMN_UPDATE_TIME = "updateTime";
	private final static String COLUMN_ID = "id";

	private final static UserMapper MAPPER = new UserMapper();

	public static UserMapper getMapper() {
		return MAPPER;
	}

	public User mapRow(ResultSet rs, int rowNum) throws SQLException {
		User user = new User();
		user.setActivity(Activity.valueOf(rs.getString(COLUMN_ACTIVITY)));
		user.setAuthority(rs.getInt(COLUMN_AUTHORITY));
		user.setBirthday(rs.getDate(COLUMN_BIRTHDAY));
		user.setCity(rs.getString(COLUMN_CITY));
		user.setCountry(rs.getString(COLUMN_COUNTRY));
		user.setCreateTime(rs.getDate(COLUMN_CREATE_TIME));
		user.setSecurityEmail(rs.getString(COLUMN_SECURITY_EMAIL));
		user.setEmail(rs.getString(COLUMN_EMAIL));
		user.setGraduateSchool(rs.getString(COLUMN_GRADUATE_SCHOOL));
		user.setJob(rs.getString(COLUMN_JOB));
		user.setLastLoginIp(rs.getString(COLUMN_LAST_LOGIN_IP));
		user.setLastLoginTime(rs.getDate(COLUMN_LAST_LOGIN_TIME));
		user.setMobilephone(rs.getString(COLUMN_MOBILE_PHONE));
		user.setMsn(rs.getString(COLUMN_MSN));
		user.setName(rs.getString(COLUMN_NAME));
		user.setNickname(rs.getString(COLUMN_NICKNAME));
		// u.setPassword(rs.getString("password"));
		user.setPortrait(rs.getString(COLUMN_PORTRAIT));
		user.setProvince(rs.getString(COLUMN_PROVINCE));
		user.setQq(rs.getString(COLUMN_QQ));
		user.setSecurityAnswer(rs.getString(COLUMN_SECURITY_ANSWER));
		user.setSecurityQuestion(rs.getString(COLUMN_SECURITY_QUESTION));
		if (rs.getString(COLUMN_SEX) != null)
			user.setSex(Sex.valueOf(rs.getString(COLUMN_SEX)));
		user.setSiteId(rs.getString(COLUMN_SITE_ID));
		user.setUpdateTime(rs.getDate(COLUMN_UPDATE_TIME));
		user.setId(rs.getLong(COLUMN_ID));
		return user;
	}
}
